package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiNiuUtils;
import org.apache.zookeeper.proto.SetMaxChildrenRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    //打印log日志，流程性的日志
    private static final Logger log = LoggerFactory.getLogger(SetmealController.class);

    @Reference//订阅业务层
    private SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){

        setmealService.add(setmeal,checkgroupIds);

        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult<Setmeal> page = setmealService.findPage(queryPageBean);

        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,page);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id){

        //获取套餐id
        Setmeal setmeal = setmealService.findById(id);

        /**
         * Result{
         *     flag:
         *     message;
         *     data:{
         *     	domain: 路径
         *     	setmeal: formData  img图片名
         *        }
         * }
         */
        //图片回显
        HashMap<Object, Object> resultMap = new HashMap<>();

        resultMap.put("domain", QiNiuUtils.DOMAIN);

        resultMap.put("setmeal",setmeal);

        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,resultMap);
    }

    /**
     * 修改套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

        setmealService.update(setmeal,checkgroupIds);

        return new Result(true,"修改套餐成功！");
    }

    /**
     * 套餐图片上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        //- 获取文件名
        String originalFilename = imgFile.getOriginalFilename();

        //获取它的后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //- 产生唯一名称，拼接后缀名， 图片名
        String imgName = UUID.randomUUID().toString() + suffix;

        try {
            //- 调用QiNiuUtils上传
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),imgName);

            //- 成功后返回图片名和域名
            //{
            //    	imgName: 图片名
            //    	domain: 域名
            //	}
            Map<String,String> resultMap = new HashMap<String,String>();
            resultMap.put("imgName",imgName);
            resultMap.put("domain",QiNiuUtils.DOMAIN);

            return new Result(true, MessageConstant.UPLOAD_SUCCESS,resultMap);
        } catch (IOException e) {
            //e.printStackTrace();
            log.error("上传文件失败",e);
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 通过套餐id查询选中的检查组id集合
     * @param id
     * @return
     */
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(int id){

        List<Integer> checkgroupIdsBySetmealId = setmealService.findCheckgroupIdsBySetmealId(id);

        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkgroupIdsBySetmealId);
    }

    /**
     * 通过id删除套餐
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id){

        setmealService.deleteById(id);

        return new Result(true,"删除成功！");
    }
}
